package com.easthome.common.log;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * 类描述：
 *  日志切面：记录请求的参数，返回的结果
 * @ClassName LogAOP
 * @Author msi
 * @Date 2020/11/4 16:13
 * @Version 1.0
 */
@Slf4j
@Aspect
@Component
public class LogAOP {
    /**
     * 手动创建的对象所在包
     */
    public static final String PACKAGE_NAME = "com.cfl.jd";

    /**
     * 全局日志输出
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around(value = "execution(* com.easthome.*.controller.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 参数值
        Object[] args = pjp.getArgs();
        Signature signature = pjp.getSignature();
        // 完全方法名（完全类名+方法名）
        String entirelyMethodName = signature.getDeclaringTypeName() + "." + signature.getName();
        // 参数名
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] parameterNames = methodSignature.getParameterNames();
        Class[] parameterTypes = methodSignature.getParameterTypes();
        // 方法返回值
//        String returnTypeName = methodSignature.getReturnType().getName();
        StringBuilder sb = new StringBuilder();
        for (int i = 0, length = args.length; i < length; i++) {
            sb.append("参数:").append(parameterNames[i]).append("(").append(parameterTypes[i].getName()).append(")")
                    .append(" ————> ");
            if (Objects.nonNull(args[i])) {
                if (parameterTypes[i].getName().indexOf(LogAOP.PACKAGE_NAME) != -1) {
                    sb.append(JSON.toJSONString(args[i]));
                } else{
                    sb.append(args[i].toString());
                }
            } else {
                sb.append("null");
            }

            if (i < length - 1) {
                sb.append("\n");
            }

        }
        if (args.length > 0) {
            log.info("方法：{} 开始执行，请求参数如下:\n{}",entirelyMethodName, sb.toString());
        }

        // 继续执行方法
        Object obj = pjp.proceed();
//        ThreadLocal<String> userNameLocal = LoginUserHelper.getUserName();
//        String userName = userNameLocal.get();
        if (Objects.nonNull(obj)) {
            String name = obj.getClass().getName();
            if (name.indexOf(LogAOP.PACKAGE_NAME) != -1) {
                log.info("方法：{} 结束执行，返回值:\n{}", entirelyMethodName, JSON.toJSONString(obj));
            } else {
                log.info("方法：{} 结束执行，返回值:\n{}", entirelyMethodName, obj.toString());
            }
//            SysOperLog sysOperLog = new SysOperLog();
//            sysOperLog.setOperName(userName);
//            sysOperLog.setJsonResult(JSON.toJSONString(obj));
//            sysOperLog.setOperTime(new Date());
//            sysOperLog.setMethod(entirelyMethodName);

        }

        return obj;
    }

}

